- Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Copy pathSolution.py
32 lines (30 loc) · 1.08 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
classSolution:
defpatternMatching(self, pattern: str, value: str) ->bool:
defcheck(la: int, lb: int) ->bool:
i=0
a, b="", ""
forcinpattern:
ifc=="a":
ifaandvalue[i : i+la] !=a:
returnFalse
a=value[i : i+la]
i+=la
else:
ifbandvalue[i : i+lb] !=b:
returnFalse
b=value[i : i+lb]
i+=lb
returna!=b
n=len(value)
cnt=Counter(pattern)
ifcnt["a"] ==0:
returnn%cnt["b"] ==0andvalue[: n//cnt["b"]] *cnt["b"] ==value
ifcnt["b"] ==0:
returnn%cnt["a"] ==0andvalue[: n//cnt["a"]] *cnt["a"] ==value
forlainrange(n+1):
ifla*cnt["a"] >n:
break
lb, mod=divmod(n-la*cnt["a"], cnt["b"])
ifmod==0andcheck(la, lb):
returnTrue
returnFalse